package com.huifer.wgx.request;

import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>Title : RequestUtil </p>
 * <p>Description : request 工具</p>
 *
 * @author huifer
 * @date 2019-03-18
 */
public class RequestUtil {


    /**
     * 获取请求 basePath
     * @param request  request
     * @return basePath
     */
    public static String getBasePath(HttpServletRequest request) {
        StringBuffer basePath = new StringBuffer();
        String scheme = request.getScheme();
        String domain = request.getServerName();
        int port = request.getServerPort();
        basePath.append(scheme);
        basePath.append("://");
        basePath.append(domain);
        if("http".equalsIgnoreCase(scheme) && 80 != port) {
            basePath.append(":").append(String.valueOf(port));
        } else if("https".equalsIgnoreCase(scheme) && port != 443) {
            basePath.append(":").append(String.valueOf(port));
        }
        return basePath.toString();
    }

    /**
     * request 移除指定参数
     *
     * @param request request
     * @param param 参数名
     * @return queryString
     */
    public String removeParam(HttpServletRequest request, String param) {
        String queryString = "";
        Enumeration parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String k = (String) parameterNames.nextElement();
            if (k.equals(param)) {
                continue;
            }
            if ("".equals(queryString)) {
                queryString = k + "=" + request.getParameter(k);
            } else {
                queryString += "&" + k + "=" + request.getParameter(k);
            }
        }
        return queryString;
    }

}
